package com.kgis.fm.business.management.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.kgis.fm.access.metadata.service.ComponentAccessService;
import com.kgis.fm.access.metadata.service.ConnectionshipAccessService;
import com.kgis.fm.access.metadata.service.FeatureAccessService;
import com.kgis.fm.access.metadata.service.MapExtentAccessService;
import com.kgis.fm.access.metadata.service.OwnershipAccessService;
import com.kgis.fm.access.metadata.service.RelationshipMappingAccessService;
import com.kgis.fm.access.term.service.LabelAccessService;
import com.kgis.fm.access.term.service.LabelRuleAccessService;
import com.kgis.fm.access.term.service.LayerParameterAccessService;
import com.kgis.fm.access.term.service.LegendEntryAccessService;
import com.kgis.fm.access.term.service.LegendEntryDefinitionAccessService;
import com.kgis.fm.access.term.service.LegendEntryDisplayControlAccessService;
import com.kgis.fm.access.term.service.LineStyleAccessService;
import com.kgis.fm.access.term.service.PointStyleAccessService;
import com.kgis.fm.access.term.service.PolygonStyleAccessService;
import com.kgis.fm.access.term.service.StyleRuleAccessService;
import com.kgis.fm.access.term.service.TextStyleAccessService;
import com.kgis.fm.business.management.model.FmMetadata;
import com.kgis.fm.business.management.service.FmMetaDataManagementService;
import com.kgis.fm.business.management.service.impl.support.SQLRuleParseSupport;
import com.kgis.fm.model.term.LabelRule;
import com.kgis.fm.model.term.LegendEntryDisplayControl;
import com.kgis.fm.model.term.StyleRule;
/**
 * 所有的元数据放到客户端缓存
 * @author zhxiaomiao
 *
 */
public class FmMetaDataManagementServiceImpl implements FmMetaDataManagementService{
	private FeatureAccessService featureAccessService;
	private ComponentAccessService componentAccessService;
	private MapExtentAccessService mapExtentAccessService;
	private LayerParameterAccessService layerParameterAccessService;
	private LegendEntryAccessService legendEntryAccessService;
	private LegendEntryDefinitionAccessService legendEntryDefinitionAccessService;
	private LegendEntryDisplayControlAccessService legendEntryDisplayControlAccessService;
	
	private PointStyleAccessService pointStyleAccessService;
	private LabelRuleAccessService labelRuleAccessService;
	private TextStyleAccessService textStyleAccessService;
	private LabelAccessService labelAccessService;
	private LineStyleAccessService lineStyleAccessService;
	private PolygonStyleAccessService polygonStyleAccessService;
	private StyleRuleAccessService styleRuleAccessService;
	
	private RelationshipMappingAccessService relationshipMappingAccessService;
	private ConnectionshipAccessService connectionshipAccessService;
	private OwnershipAccessService ownershipAccessService;
	@Override
	public FmMetadata findAllMetadata() {
		FmMetadata metadata = new FmMetadata();
		metadata.setFeatureList(featureAccessService.findAll());
		metadata.setComponentList(componentAccessService.findAll());
		metadata.setMapExtentList(mapExtentAccessService.findAll());
		metadata.setLayerParameterList(layerParameterAccessService.findAll());
		metadata.setLegendEntryList(legendEntryAccessService.findAll());
		metadata.setLegendEntryDefList(legendEntryDefinitionAccessService.findAll());

		metadata.setPointStyleList(pointStyleAccessService.findAll());
		metadata.setPolylineStyleList(lineStyleAccessService.findAll());
		metadata.setPolygonStyleList(polygonStyleAccessService.findAll());
		metadata.setLabelStyleList(textStyleAccessService.findAll());
		metadata.setLabelList(labelAccessService.findAll());
		
		
		List<StyleRule>styleRuleList=styleRuleAccessService.findAll();
		for (StyleRule styleRule : styleRuleList) {
			styleRule.setFilter(SQLRuleParseSupport.SQLRuleFlexParse(styleRule.getFilter()));
		}
		metadata.setStyleRuleList(styleRuleList);
		List<LabelRule>labelRuleList=labelRuleAccessService.findAll();
		for (LabelRule labelRule : labelRuleList) {
			labelRule.setFilter(SQLRuleParseSupport.SQLRuleFlexParse(labelRule.getFilter()));
		}
		metadata.setLabelRuleList(labelRuleList);

		
		List<LegendEntryDisplayControl> legendEntryDisplayList = legendEntryDisplayControlAccessService.findAll();
		Map<Integer, List<LegendEntryDisplayControl>> map = new HashMap<Integer, List<LegendEntryDisplayControl>>();
		List<LegendEntryDisplayControl> listForLegend;
		for (LegendEntryDisplayControl legendEntryDisplayControl : legendEntryDisplayList) {
			int key = legendEntryDisplayControl.getLno();
			if (map.get(key) != null) {
				map.get(key).add(legendEntryDisplayControl);
			} else {
				listForLegend = new ArrayList<LegendEntryDisplayControl>();
				listForLegend.add(legendEntryDisplayControl);
				map.put(key, listForLegend);
			}
		}
		metadata.setLegendEntryDisplayMap(map);
		
		metadata.setRelationshipMappingList(relationshipMappingAccessService.findAll());
		metadata.setConnectionshipList(connectionshipAccessService.findAll());
		metadata.setOwnershipList(ownershipAccessService.findAll());
		
		return metadata;
	}

	public void setLayerParameterAccessService(LayerParameterAccessService layerParameterAccessService) {
		this.layerParameterAccessService = layerParameterAccessService;
	}

	public LayerParameterAccessService getLayerParameterAccessService() {
		return layerParameterAccessService;
	}

	public void setMapExtentAccessService(MapExtentAccessService mapExtentAccessService) {
		this.mapExtentAccessService = mapExtentAccessService;
	}

	public MapExtentAccessService getMapExtentAccessService() {
		return mapExtentAccessService;
	}

	public FeatureAccessService getFeatureAccessService() {
		return featureAccessService;
	}

	public void setFeatureAccessService(FeatureAccessService featureAccessService) {
		this.featureAccessService = featureAccessService;
	}

	public ComponentAccessService getComponentAccessService() {
		return componentAccessService;
	}

	public void setComponentAccessService(ComponentAccessService componentAccessService) {
		this.componentAccessService = componentAccessService;
	}

	public void setLegendEntryDisplayControlAccessService(LegendEntryDisplayControlAccessService legendEntryDisplayControlAccessService) {
		this.legendEntryDisplayControlAccessService = legendEntryDisplayControlAccessService;
	}

	public LegendEntryDisplayControlAccessService getLegendEntryDisplayControlAccessService() {
		return legendEntryDisplayControlAccessService;
	}

	public void setLegendEntryAccessService(LegendEntryAccessService legendEntryAccessService) {
		this.legendEntryAccessService = legendEntryAccessService;
	}

	public LegendEntryAccessService getLegendEntryAccessService() {
		return legendEntryAccessService;
	}

	public LegendEntryDefinitionAccessService getLegendEntryDefinitionAccessService() {
		return legendEntryDefinitionAccessService;
	}

	public void setLegendEntryDefinitionAccessService(LegendEntryDefinitionAccessService legendEntryDefinitionAccessService) {
		this.legendEntryDefinitionAccessService = legendEntryDefinitionAccessService;
	}

	public PointStyleAccessService getPointStyleAccessService() {
		return pointStyleAccessService;
	}

	public void setPointStyleAccessService(PointStyleAccessService pointStyleAccessService) {
		this.pointStyleAccessService = pointStyleAccessService;
	}

	public LabelRuleAccessService getLabelRuleAccessService() {
		return labelRuleAccessService;
	}

	public void setLabelRuleAccessService(LabelRuleAccessService labelRuleAccessService) {
		this.labelRuleAccessService = labelRuleAccessService;
	}

	public TextStyleAccessService getTextStyleAccessService() {
		return textStyleAccessService;
	}

	public void setTextStyleAccessService(TextStyleAccessService textStyleAccessService) {
		this.textStyleAccessService = textStyleAccessService;
	}

	public LineStyleAccessService getLineStyleAccessService() {
		return lineStyleAccessService;
	}

	public void setLineStyleAccessService(LineStyleAccessService lineStyleAccessService) {
		this.lineStyleAccessService = lineStyleAccessService;
	}

	public PolygonStyleAccessService getPolygonStyleAccessService() {
		return polygonStyleAccessService;
	}

	public void setPolygonStyleAccessService(PolygonStyleAccessService polygonStyleAccessService) {
		this.polygonStyleAccessService = polygonStyleAccessService;
	}

	public StyleRuleAccessService getStyleRuleAccessService() {
		return styleRuleAccessService;
	}

	public void setStyleRuleAccessService(StyleRuleAccessService styleRuleAccessService) {
		this.styleRuleAccessService = styleRuleAccessService;
	}

	public LabelAccessService getLabelAccessService() {
		return labelAccessService;
	}

	public void setLabelAccessService(LabelAccessService labelAccessService) {
		this.labelAccessService = labelAccessService;
	}

	public RelationshipMappingAccessService getRelationshipMappingAccessService() {
		return relationshipMappingAccessService;
	}

	public void setRelationshipMappingAccessService(RelationshipMappingAccessService relationshipMappingAccessService) {
		this.relationshipMappingAccessService = relationshipMappingAccessService;
	}

	public ConnectionshipAccessService getConnectionshipAccessService() {
		return connectionshipAccessService;
	}

	public void setConnectionshipAccessService(ConnectionshipAccessService connectionshipAccessService) {
		this.connectionshipAccessService = connectionshipAccessService;
	}

	public OwnershipAccessService getOwnershipAccessService() {
		return ownershipAccessService;
	}

	public void setOwnershipAccessService(OwnershipAccessService ownershipAccessService) {
		this.ownershipAccessService = ownershipAccessService;
	}

	@Override
	public int getLogicMapLayerIdByEntityTypeId(int fno, int cno) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getMapLayerIdByEntityTypeId(int fno, int cno) {
		// TODO Auto-generated method stub
		return 0;
	}
	
}
